import { encodeAll } from "./codec/codec";
import { CodecBase } from "./codec/codec_type";

export enum Type {
    ACK = 0,
    PING = 1,
    PONG = 2,
    SIMPLE_REQ = 3,
    SIMPLE_RES = 4,
    SESSION = 5,
    REQ = 6,
    RES = 7,
    NOTIFY = 8,
    NOTIFY_ACK = 9,
}

export enum Cmd {
    STATUS = 0,
    LOG = 1,
    OTA_START = 2,
    OTA_TRANSLATE = 3,
    OTA_COMPLETE = 4,
    GET_TASK_INFO = 5,
    PARTS_CTRL = 6,
    NOZZLE_CTRL = 7,
    DOOR_CTRL = 8,
    SCAN = 9,
    PARAM = 0x0A,
    DRAW_HEAT_CTRL = 0x0B,
    DRAW_WATER_CTRL = 0x0C,
}


export function makeProto(type: number, seq: number, ...args: CodecBase[]) {
    let len = 7;
    for (let arg of args) {
        len += arg.size();
    }
    const buf: number[] = new Array(len);
    buf[0] = 0xE1;
    buf[1] = 0x1E;
    buf[2] = len & 0xFF;
    buf[3] = type;
    buf[4] = seq;
    encodeAll(buf, 5, ...args);
    let sum = 0;
    for (let i = 3; i < (len - 2); i++) {
        sum = sum ^ buf[i];
    }
    buf[len - 2] = sum;
    buf[len - 1] = 0xEF;
    return buf;
}

